package com.tqfframe.studydemo.myDubboRpc.core;

import com.tqfframe.studydemo.myDubboRpc.core.handler.ServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Proxy;
import java.util.Properties;

/**
 * Created by Tang-QiFeng on 2022/2/18
 */
public class RpcServer implements Server{

    private Logger logger=LoggerFactory.getLogger(this.getClass());

    final static String  RPC_SERVER_PORT="DUBBO_RPC_SERVER_PORT";
    //rpc服务接口
    final static String  RPC_SERVER_INTERFACE="DUBBO_RPC_SERVER_INTERFACE";


    @Override
    public void Handler() throws IOException {
            //读取配置文件
            Properties properties = new Properties();
            InputStream resourceAsStream = com.tqfframe.studydemo.mySocketRpc.core.RpcServer.class.getClassLoader().getResourceAsStream("rpc/RpcServerConfig.properties");
            properties.load(resourceAsStream);
            Integer port =new Integer((String) properties.get(RPC_SERVER_PORT));
            String rpc_interface =(String) properties.get(RPC_SERVER_INTERFACE);


            try {
                // Configure the server.
                /*步骤
                 * 创建一个ServerBootstrap b实例用来配置启动服务器
                 * b.group指定NioEventLoopGroup来接收处理新连接
                 * b.channel指定通道类型
                 * b.option设置一些参数
                 * b.handler设置日志记录
                 * b.childHandler指定连接请求，后续调用的channelHandler
                 * b.bind设置绑定的端口
                 * b.sync阻塞直至启动服务
                 */
                ServerBootstrap bootstrap = new ServerBootstrap();
                NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
                bootstrap.group(eventLoopGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast(new StringDecoder());
                                p.addLast(new StringEncoder());
                                 //重要！！！这里定义了服务端处理类。
                                p.addLast(new ServerHandler(rpc_interface));
                            }
                        });
                bootstrap.bind("localhost", port).sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        logger.info("netty servier start success...."+port);

        }
}
